package com.mxx.common.generator;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Objects;

@Data
@Accessors(chain = true)
public class AnnoValue {

    public String key;

    private Type type;

    private Object data;

    public static AnnoValue value(String key, Type type, Object data) {
        Objects.requireNonNull(key);
        Objects.requireNonNull(type);
        Objects.requireNonNull(data);
        return new AnnoValue().setData(data).setKey(key).setType(type);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(key).append(" = ");
        switch (type) {
            case ANNO:
                sb.append(data.toString());
            case OTHER:
                sb.append(data);
                break;
            case STRING:
                sb.append("\"").append(data).append("\"");
                break;
            default:

                Object[] dataArr = (Object[]) data;
                sb.append("[");
                switch (type) {
                    case ARRAY_STRING:
                        for (Object v : dataArr) {
                            sb.append("\"").append(v).append("\"").append(",");
                        }
                    case ARRAY_ANNO:
                    case ARRAY_OTHER:
                        for (Object v : dataArr) {
                            sb.append(v).append(",");
                        }
                }
                sb.append("]");
                break;
        }
        return sb.toString();

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AnnoValue that = (AnnoValue) o;
        return key.equals(that.key);
    }

    @Override
    public int hashCode() {
        return Objects.hash(key);
    }

    public static enum Type {
        STRING,
        OTHER,
        ANNO,
        ARRAY_STRING,
        ARRAY_OTHER,
        ARRAY_ANNO,
        ;

    }
}
